﻿using System;
using System.Text;
using System.Threading;
using SinaApi;
using SurrealTodoAdapter;
using YGD.MicroBlogAnalysis;
using YGD.Generic;
using System.Linq;


namespace YGD.Services
{
    class Program
    {
        static void Main(string[] args)
        {
            DoCheck(null);
            SingleTest();
            var timer = new Timer(DoCheck, null, 2000, 30000);
            var timer1 = new Timer(DoTiming, null, 2000, 60000);
            Console.ReadKey();
            timer.Dispose();
            timer1.Dispose();

            //DoCheck(null);
            //DoTiming(null);
            Console.ReadKey();
        }

        private static void DoTiming(object state)
        {
            try
            {

                DateTime dateTimeNow = DateTime.Now;
                var surrealTodoService = new SurrealTodoService();
                var sinaService = new SinaBlogApiService();
                var timingTasks = surrealTodoService.GetTimings();
                foreach (var timingTask in timingTasks)
                {
                    var timeSpan = timingTask.timing - dateTimeNow;
                    //sinaService.ToUpdate("@" + timingTask.user + " 你定时了: " + timingTask.text + DateTime.Now.Ticks);

                    if (timeSpan.TotalMinutes == 1440)
                    {
                        sinaService.ToUpdate("你的日程: " + timingTask.text + " 将于明天到期!");
                    }
                    else if (timeSpan.TotalMinutes == 120)
                    {
                        sinaService.ToUpdate("你的日程: " + timingTask.text + " 将于两个小时后到期!");
                    }
                }
            }
            catch (Exception exp)
            {
                LogEx.ERROR("", exp.Message, exp);
            }
        }

        private static void DoCheck(object state)
        {
            try
            {
                var dataStorage = new DataStorage();
                var lasttime = dataStorage.Load<DateTime>("lasttime", "config");
                LogEx.TRACE("", "lasttime" + lasttime);

                var sinaService = new SinaBlogApiService();
                var surrealTodoService = new SurrealTodoService();
                var analysisService = new MicroBlogAnalysis.MicroBlogAnalysis();

                var statuses = sinaService.GetStatuses();

                LogEx.TRACE("", "Get statuses" + statuses.Count);

                foreach (var status in statuses.Where(s => s.created_date > lasttime))
                {
                    TaskBase taskBase = analysisService.AnalysisTask(status.text, status.created_date);

                    //用户有可能不是指派任务，测试检测到空的内容，忽略
                    if (string.IsNullOrEmpty(taskBase.Content)) continue;

                    if (taskBase.IsTimingTask)
                        surrealTodoService.AddNewTask(status.user.screen_name, taskBase.Content, taskBase.Timing);
                    else
                        surrealTodoService.AddNewTask(status.user.screen_name, taskBase.Content);

                    var tasks = surrealTodoService.GetToDoTasks(status.user.screen_name);

                    var commentContent = new StringBuilder();
                    commentContent.Append("等待你搞定:");
                    for (int i = 0; i < tasks.Count; i++)
                    {
                        commentContent.Append((i + 1) + ". " + tasks[i].text);
                    }
                    sinaService.ToComment(status.id, commentContent.ToString().CutString(139));
                }
                LogEx.TRACE("", "Save lasttime as flag");
                lasttime = statuses.Max(s => s.created_date);
                dataStorage.Save(lasttime, "lasttime", "config");
                LogEx.TRACE("", "lasttime" + lasttime);
            }
            catch (Exception exp)
            {
                LogEx.ERROR("", exp.Message, exp);
            }
        }

        static void SingleTest()
        {
            var sinaService = new SinaBlogApiService();
            var surrealTodoService = new SurrealTodoService();
            var analysisService = new MicroBlogAnalysis.MicroBlogAnalysis();


            var statuses = sinaService.GetStatuses();
            foreach (var status in statuses)
            {
                TaskBase taskBase = analysisService.AnalysisTask(status.text, status.created_date);

                //用户有可能不是指派任务，测试检测到空的内容，忽略
                if (string.IsNullOrEmpty(taskBase.Content)) return;

                surrealTodoService.AddNewTask(status.user.screen_name, taskBase.Content);

                var tasks = surrealTodoService.GetToDoTasks(status.user.screen_name);
                var commentContent = new StringBuilder();
                for (int i = 0; i < tasks.Count; i++)
                {
                    commentContent.Append((i + 1) + tasks[i].text);
                }

                sinaService.ToComment(status.id, commentContent.ToString());
            }
        }
    }
}
